﻿/****************************************************************
 *
 * www.payitforward.edu.vn
 * 
 ****************************************************************/
/****************************************************************
 *
 * Tot nghiep C6
 * 
 ****************************************************************/
/****************************************************************
 *
 *	Module		: Wuynh chuot
 *	Description	: Relaxtion
 *  Tool		: HI-TECH PIC
 *	Chip		: 16F887
 * 	History		: 25/07/2012
 *	Show		: 05/08/2012			
 *	Author		: Pham Van Tuan	 
 *	Notes		:
 *				
 *
 ****************************************************************/
/****************************************************************
* IMPORT
****************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Xml;

namespace chuot_chui
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int s=0;
        char a;
        int b;
        int Start = 0;
        int intlen = 0; // luu gia tri so Com ket noi vao may tinh
        private void timer1_Tick(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames(); //lay tat ca cong COM
            if (intlen != ports.Length)
            {
                intlen = ports.Length;
                CbsecCom.Items.Clear();
                for (int j = 0; j < intlen; j++)
                {
                    CbsecCom.Items.Add(ports[j]);
                }
            }
            txtreceive.Text = Convert.ToString(s);
            if(Start==1)
            {
                if (button1.BackColor == Color.Red)
                {
                    button3.BackColor = Color.Gray;
                    button2.BackColor = Color.Gray;
                    button1.BackColor = Color.Gray;
                    button4.BackColor = Color.Red;
                    a = '4';
                }
                else
                {
                    if (button4.BackColor == Color.Red)
                    {
                        button1.BackColor = Color.Gray;
                        button2.BackColor = Color.Red;
                        a = '2';
                        button3.BackColor = Color.Gray;
                        button4.BackColor = Color.Gray;
                    }
                    else
                    {
                        if (button2.BackColor == Color.Red)
                        {
                            button2.BackColor = Color.Gray;
                            button1.BackColor = Color.Gray;
                            button3.BackColor = Color.Red;
                            a = '3';
                            button4.BackColor = Color.Gray;
                        }
                        else
                        {
                            if (button3.BackColor == Color.Red)
                            {
                                button3.BackColor = Color.Gray;
                                button1.BackColor = Color.Red;
                                button2.BackColor = Color.Gray;
                                button4.BackColor = Color.Gray;
                                a = '1';
                            }
                            else
                            {
                                button4.BackColor = Color.Gray;
                                button1.BackColor = Color.Red;
                                a = '1';
                            }
                        }
                    }
               }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
        // khoi dong game
        private void button5_Click(object sender, EventArgs e)
        {
            s = 0;
            if (CbsecCom.Text == "")
                MessageBox.Show("Chua chon vu khi", "Thong Bao");
            else
            {
                if (lbstatus.Text == "Disconnect")
                {
                    Com.PortName = CbsecCom.Text;
                    Com.Open();
                    lbstatus.Text = "Connect";
                }
                while (button5.BackColor == Color.Blue) ;
                button5.BackColor = Color.Blue;
                button6.BackColor = Color.Gray;
                button1.BackColor = Color.Gray;
                button2.BackColor = Color.Gray;
                button3.BackColor = Color.Gray;
                button4.BackColor = Color.Gray;
                timer1.Start();
                Start = 1;
            }
        }
        //dung game
        private void button6_Click(object sender, EventArgs e)
        {
            if (lbstatus.Text == "Connect") 
            {
                Com.Close();
                lbstatus.Text = "Disconnect";
            }
            while (button6.BackColor == Color.Red) ;
            button6.BackColor = Color.Red;
            button5.BackColor = Color.Gray;
            button1.BackColor = Color.Pink;
            button2.BackColor = Color.Pink;
            button3.BackColor = Color.Pink;
            button4.BackColor = Color.Pink;
            timer1.Stop();
        }
        //chuong trinh con ktra cong diem neu dung
        private void OnCom(object sender, SerialDataReceivedEventArgs e)
        {

            b = Com.ReadByte();
            if (b == a)
            {
                s++;
            }

        }
        //hien diem
        private void Display(int s)
        {
            throw new NotImplementedException();
        }
        private delegate void DlDisplay(string s);
        private void Display(string s)
        {
            if (txtreceive.InvokeRequired)
            {
                DlDisplay sd = new DlDisplay(Display);
                txtreceive.Invoke(sd, new object[] { s });
            }
            else
            {
                txtreceive.Text = s;
            }
        }
    }
}

/****************************************************************
 * THE END
 ****************************************************************/